<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('订单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>订单金额：</label>
                            <input type="text" name="orderAmount"/>
                        </li>
                        <li>
                            <label>买单客户：</label>
                            <input type="text" name="orderCustomer"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:orders:add">
                <i class="fa fa-plus"></i> 添加
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:orders:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:orders:remove')}]];
    var statusDatas = [[${@dict.getType('order_type')}]];
    var prefix = ctx + "system/orders";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            readUrl: prefix + "/read/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "",
            pagination: true,            // 必须显式开启
            pageSize: 10,                // 默认分页大小
            pageList: [10, 25, 50],      // 可选分页大小
            showPageGo: true,            // 显示快速跳转
            showRefresh: true,           // 显示刷新按钮
            sidePagination: "server",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1; // 表格的索引从0开始，因此加1
                    }
                },
                {
                    field: 'orderAmount',
                    title: '订单金额'
                },
                {
                    field: 'orderCustomer',
                    title: '买单客户'
                },
                {
                    title: '订单时间',
                    formatter: function (value, row, index) {
                        return new Date().toISOString().split('T')[0];
                    }
                },
                {
                    field: 'status',
                    title: '订单状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // 新增状态操作按钮
                        if (row.isAdmin) {
                            actions.push('<a class="btn btn-primary btn-xs" onclick="shipOrder(\'' + row.id + '\')"><i class="fa fa-truck"></i>发货</a> ');
                        } else {
                            actions.push('<a class="btn btn-info btn-xs" onclick="confirmReceipt(\'' + row.id + '\')"><i class="fa fa-check-circle"></i>确认收货</a> ');
                        }
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.read(\'' + row.id + '\')"><i class="fa fa-edit"></i>订单详情</a> ');

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 发货操作
    function shipOrder(orderId) {
        $.modal.confirm("确认要发货该订单吗？", function() {
            var url = prefix + "/ship/" + orderId;
            $.operate.submit(url, "post", null, function(result) {
                $.table.refresh();
                $.modal.alertSuccess("发货成功");
            });
        });
    }

    // 确认收货操作
    function confirmReceipt(orderId) {
        $.modal.confirm("确认已完成收货吗？", function() {
            var url = prefix + "/confirm/" + orderId;
            $.operate.submit(url, "post", null, function(result) {
                $.table.refresh();
                $.modal.alertSuccess("收货确认成功");
            });
        });
    }

</script>
</body>
</html>